package com.shangxia.leetcode.greedyAlgorithm;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: leetcode
 * @ClassName: PartitionLabels763
 * @Time: 2020/12/6
 * @Author: zhouy
 * @Description: 763. 划分字母区间
 * @Experience:
 */

@Slf4j
public class PartitionLabels763 {
    public static void main(String[] args) {

    }

    public static List<Integer> partitionLabels(String S) {
        int[] last = new int[26];
        int length = S.length();
        for (int i = 0; i < length; i++) {
            last[S.charAt(i) - 'a'] = i;
        }
        List<Integer> partition = new ArrayList<>();
        int start = 0, end = 0;
        for (int i = 0; i < length; i++) {
            end = Math.max(end, last[S.charAt(i) - 'a']);
            if (i == end) {
                partition.add(end - start + 1);
                start = end + 1;
            }
        }
        return partition;
    }

}
